<?php

namespace App\Models;

use App\Models\Traits\HasCreatedByAdminIdAttribute;
use App\Models\Traits\HasGroupScope;

/**
 * App\Models\AdminUsersModel
 *
 * @property int $id
 * @property string|null $account
 * @property string $name
 * @property string|null $phone
 * @property string|null $email
 * @property string $password
 * @property string|null $pay_password 支付密码
 * @property string|null $salt
 * @property string|null $pay_salt 支付密码盐
 * @property string $head_img 头像
 * @property string|null $remember_token
 * @property int|null $role_id 角色id
 * @property \Illuminate\Support\Carbon|null $created_at
 * @property \Illuminate\Support\Carbon|null $updated_at
 * @property string|null $deleted_at
 * @property int $group_id 组织架构
 * @property int|null $is_valid 是否禁用
 * @property string|null $menu_id 功能ID集合，逗号隔开
 * @property string|null $routes_id 路由集合
 * @property int|null $is_open 开放权限
 * @property string|null $last_login_time 上次登录时间
 * @property string|null $now_login_time 本次登录时间
 * @property string|null $last_login_ip 上次登录密码
 * @property string|null $now_login_ip 本次登录IP
 * @property int|null $sms_login 是否开启短信验证登录 1开启 0不开启
 * @property int $user_type 1、系统管理员 2.普通管理员
 * @property int $created_by_admin_id 创建人
 * @property int $updated_by_admin_id 更新人
 * @property int|null $temp_group_id 临时组织（用于系统超级管理员切换组织）
 * @property string $guards 区分是哪个管理端
 * @property string|null $jpush_registration_id 手机app jpush推送
 * @property int|null $data_center_admin_user_id 数据中心后台用户id
 * @property int|null $old_id
 * @property \App\Models\AdminUsers|null $createdAdmin
 * @property \App\Models\AdminGroupModel|null $group
 * @property \Illuminate\Database\Eloquent\Collection<int, \App\Models\AdminUsersRolesModel> $role_info
 * @property int|null $role_info_count
 * @method static \Illuminate\Database\Eloquent\Builder|AdminUsersModel newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|AdminUsersModel newQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|BaseModel paginateFilter($perPage = null, $columns = [], $pageName = 'page', $page = null)
 * @method static \Illuminate\Database\Eloquent\Builder|AdminUsersModel query()
 * @method static \Illuminate\Database\Eloquent\Builder|BaseModel simplePaginateFilter($perPage = null, $columns = [], $pageName = 'page', $page = null)
 * @method static \Illuminate\Database\Eloquent\Builder|AdminUsersModel whereAccount($value)
 * @method static \Illuminate\Database\Eloquent\Builder|BaseModel whereBeginsWith($column, $value, $boolean = 'and')
 * @method static \Illuminate\Database\Eloquent\Builder|AdminUsersModel whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|AdminUsersModel whereCreatedByAdminId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|AdminUsersModel whereDataCenterAdminUserId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|AdminUsersModel whereDeletedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|AdminUsersModel whereEmail($value)
 * @method static \Illuminate\Database\Eloquent\Builder|BaseModel whereEndsWith($column, $value, $boolean = 'and')
 * @method static \Illuminate\Database\Eloquent\Builder|AdminUsersModel whereGroupId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|AdminUsersModel whereGuards($value)
 * @method static \Illuminate\Database\Eloquent\Builder|AdminUsersModel whereHeadImg($value)
 * @method static \Illuminate\Database\Eloquent\Builder|AdminUsersModel whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|AdminUsersModel whereIsOpen($value)
 * @method static \Illuminate\Database\Eloquent\Builder|AdminUsersModel whereIsValid($value)
 * @method static \Illuminate\Database\Eloquent\Builder|AdminUsersModel whereJpushRegistrationId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|AdminUsersModel whereLastLoginIp($value)
 * @method static \Illuminate\Database\Eloquent\Builder|AdminUsersModel whereLastLoginTime($value)
 * @method static \Illuminate\Database\Eloquent\Builder|BaseModel whereLike($column, $value, $boolean = 'and')
 * @method static \Illuminate\Database\Eloquent\Builder|AdminUsersModel whereMenuId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|AdminUsersModel whereName($value)
 * @method static \Illuminate\Database\Eloquent\Builder|AdminUsersModel whereNowLoginIp($value)
 * @method static \Illuminate\Database\Eloquent\Builder|AdminUsersModel whereNowLoginTime($value)
 * @method static \Illuminate\Database\Eloquent\Builder|AdminUsersModel whereOldId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|AdminUsersModel wherePassword($value)
 * @method static \Illuminate\Database\Eloquent\Builder|AdminUsersModel wherePayPassword($value)
 * @method static \Illuminate\Database\Eloquent\Builder|AdminUsersModel wherePaySalt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|AdminUsersModel wherePhone($value)
 * @method static \Illuminate\Database\Eloquent\Builder|AdminUsersModel whereRememberToken($value)
 * @method static \Illuminate\Database\Eloquent\Builder|AdminUsersModel whereRoleId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|AdminUsersModel whereRoutesId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|AdminUsersModel whereSalt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|AdminUsersModel whereSmsLogin($value)
 * @method static \Illuminate\Database\Eloquent\Builder|AdminUsersModel whereUpdatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|AdminUsersModel whereUpdatedByAdminId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|AdminUsersModel whereUserType($value)
 * @mixin \Eloquent
 */
class AdminUsersModel extends BaseModel
{
    use HasCreatedByAdminIdAttribute;

    protected $table = 'admin_user';

    public function filter($data)
    {
        //
        $where = [];
        if (isset($data['id'])) {
            $where[] = ['id', '=', $data['id']];
        }
        if (isset($data['account'])) {
            $where[] = ['account', '=', $data['account']];
        }
        if (isset($data['name'])) {
            $where[] = ['name', 'like', "%" . $data['name'] . "%"];
        }
        if (isset($data['phone'])) {
            $where[] = ['phone', '=', $data['phone']];
        }
        if (isset($data['group_id'])) {
            $where[] = ['group_id', '=', $data['group_id']];
        }

        if (isset($data['guards'])) {
            $where[] = ['guards', '=', $data['guards']];
        }
        if (isset($data['is_valid'])) {
            $where[] = ['is_valid', '=', $data['is_valid']];
        }
        $this->getQueryByModel()->where($where);

        if ($this->getQueryByModel()->hasNamedScope("filter") && class_exists($this->getModelFilterClass())) {
            $this->getQueryByModel()->filter($data);
        }

        return $this->getQueryByModel();
    }


    public function getGreatedByUserInfo()
    {
        return $this->hasOne('App\Models\AdminUsersModel', 'id', 'created_by_admin_id');
    }

    public function role_info()
    {
        return $this->hasMany('App\Models\AdminUsersRolesModel', 'admin_user_id', 'id')->with('adminRoleInfo');
    }

}
